<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>专业新增</title>
    <%@include file="../../sys_common_head.jsp"%>
    <script src="<%=request.getContextPath()%>/component/jquery.min.js"></script>
    <script src="<%=request.getContextPath()%>/component/ajaxForm.js"></script>

</head>
<body>
<form class="layui-form" action="<%=request.getContextPath()%>/student/major/save" method="post" id="ajaxForm">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">专业名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="majorName" name="majorName"  autocomplete="off" placeholder="请输入专业名称"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">学校</label>
                    <div class="layui-input-block">
                        <%--<input type="text" id="schoolId" name="schoolId"  autocomplete="off" placeholder="请输入学校id"--%>
                               <%--class="layui-input">--%>
                        <select id="schoolId" name="schoolId" lay-filter="schoolId" lay-verify="required" required>
                            <option value="1">江西赣州技师学院</option>
                            <option value="2">赣州农业学校</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">层级</label>
                    <div class="layui-input-block">
                        <select name="levelId" id="levelId" lay-filter="levelId"  lay-verify="required" required>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">计划人数</label>
                    <div class="layui-input-block">
                        <input type="text" id="planNumber" name="planNumber"  autocomplete="off" placeholder="请输入计划人数"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分数线</label>
                    <div class="layui-input-block">
                        <input type="text" id="minScore" name="minScore"  autocomplete="off" placeholder="请输入分数线"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">负责人</label>
                    <div class="layui-input-block">
                        <input type="text" id="fzr" name="fzr"  autocomplete="off" placeholder="请输入负责人"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" id="lxr" name="lxr"  autocomplete="off" placeholder="请输入联系人"
                               class="layui-input">
                    </div>
                </div>


                <div id="sf_items">

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">收费项：</label>
                            <div class="layui-input-inline" style="width: 300px;">
                                <select id="charges_name" name="charges_name">

                                </select>
                            </div>
                            <label class="layui-form-label">金额：</label>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" name="money" placeholder="￥" autocomplete="off" class="layui-input">
                            </div>
                            <label class="layui-form-label">
                                <button class="layui-btn layui-btn-xs layui-btn-normal" lay-filter="formAddItem" lay-submit="" type="button">添加项</button>
                            </label>
                            <label class="layui-form-label">
                                <button class="layui-btn layui-btn-danger layui-btn-xs layui-btn-normal" lay-filter="formRemoveItem" lay-submit="" type="button">删除项</button>
                            </label>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="data-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script>
    layui.use(['form', 'jquery','laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#birthday' //指定元素
        });

        let item_js_JF=${typesAll};

        window.initSelect = function(){
            var html = "";
            for(var i = 0;i < item_js_JF.length;i++){
                html = html + '<option value="'+item_js_JF[i].code+'">'+item_js_JF[i].name+'</option>';
            }
            return html;
        }

        // 初始化层级
        window.initLevel = function(schoolId){
            $.ajax({
                url: '<%=request.getContextPath()%>/common/queryLevelBySchoolId',
                dataType: 'json',
                data:{schoolId:schoolId},
                type: 'get',
                success: function (result) {
                    var inHtml = "<option>请选择</option>";
                    for(var i = 0;i < result.length; i++){
                        inHtml = inHtml+"<option value='"+result[i].id+"'>"+result[i].levelName+"</option>"
                    }
                    $("#levelId").html(inHtml);
                    form.render('select'); //刷新select选择框渲染
                }
            })
        }

        initLevel(1)

        form.on('select(schoolId)', function(data){
            initLevel($("#schoolId").val())
        });

        // 初始化专业
        window.initMajor = function(levelId){
            $.ajax({
                url: 'common/queryMajorByLevelId',
                dataType: 'json',
                data:{levelId:levelId},
                type: 'get',
                success: function (result) {
                    majorList = result;
                    var inHtml = "<option>请选择</option>";
                    for(var i = 0;i < result.length; i++){
                        inHtml = inHtml+"<option value='"+result[i].id+"'>"+result[i].majorName+"</option>"
                    }
                    $("#majorId").html(inHtml);
                    form.render('select'); //刷新select选择框渲染
                }
            })
        }

        $("#charges_name").html(initSelect())
        form.render(); //更新全部

        window.initItem = function(){
            var htmlSelect = initSelect();
            var html = '<div class="layui-form-item">\n' +
                '                        <div class="layui-inline">\n' +
                '                            <label class="layui-form-label">收费项：</label>\n' +
                '                            <div class="layui-input-inline" style="width: 300px;">\n' +
                '                                <select name="charges_name">\n' +htmlSelect+'</select>\n' +
                '                            </div>\n' +
                '                            <label class="layui-form-label">金额：</label>\n' +
                '                            <div class="layui-input-inline" style="width: 100px;">\n' +
                '                                <input type="text" name="money" placeholder="￥" autocomplete="off" class="layui-input">\n' +
                '                            </div>\n' +
                '                            <label class="layui-form-label">\n' +
                '                                <button class="layui-btn layui-btn-xs layui-btn-normal" lay-filter="formAddItem" lay-submit="" type="button">添加项</button>\n' +
                '                            </label>\n' +
                '                        </div>\n' +
                '                    </div>';

            $("#sf_items").append(html);
        }




        form.on('submit(formAddItem)', function (data) {
            initItem();
            form.render(); //更新全部
        });

        form.on('submit(formRemoveItem)', function (data) {
            $("#sf_items").children(":last-child").css("color","red");
            $("#sf_items>div:last").remove();
            form.render(); //更新全部
        });

        form.on('submit(data-save)', function (data) {
            console.log(data)

            $("#ajaxForm").ajaxSubmit({
                type : 'post',
                url : "<%=request.getContextPath()%>/student/major/save",
                dataType:'json',
                //data:  //注意只要是写在表单里面的，都不需要加这个属性。在controller中可以根据@RequestParam String str获取到属性值。
                //contentType : "application/x-www-form-urlencoded; charset=utf-8",
                success: function(data) {
                    if(data.code == 0){
                        layer.alert("新增成功", {icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            // 可以对父窗口进行刷新
                            parent.layui.table.reload("data-table");
                        });
                    }else{
                        layer.alert(data.msg, {icon: 5},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            // 可以对父窗口进行刷新
                            parent.layui.table.reload("data-table");
                        });
                    }
                },
                error: function (data)//服务器响应失败处理函数
                {
                    layer.msg('返回数据不对');
                }
            });

            <%--$.ajax({--%>
                <%--url: '<%=request.getContextPath()%>/student/major/save',--%>
                <%--data: data.field,--%>
                <%--dataType: 'json',--%>
                <%--contentType: 'application/json',--%>
                <%--type: 'post',--%>
                <%--success: function (result) {--%>
                    <%--if (result.code == 0) {--%>
                        <%--layer.msg(result.msg, {icon: 1, time: 1000}, function () {--%>
                            <%--parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页--%>
                            <%--parent.layui.table.reload("data-table");--%>
                        <%--});--%>
                    <%--} else {--%>
                        <%--layer.msg(result.msg, {icon: 2, time: 1000});--%>
                    <%--}--%>
                <%--}--%>
            <%--})--%>
            return false;
        });
    })
</script>

</body>
</html>